
// create a global large canvas element to get imagedata.
// if need image data visit. then paint img to canvas, then got img, then cache for later use.

function ImageWrapper(url, mgr)
{
    if (this instanceof ImageWrapper)
    {
        this.loaded = false;
        this.image = new Image();
        this.width = 0;
        this.height = 0;
        var wrapper_this = this;
        this.image.addEventListener("load", function() {wrapper_this.onload();}, false);
        this.imgMgr = null;
        if (mgr !== undefined)
        {
            this.imgMgr = mgr;
        }
        
        this.image.src = url;
        this.imageData = null;
    }
    else
    {
        return new ImageWrapper(url, mgr);
    }
}

ImageWrapper.prototype.testNotAlpha = function(pt)
{
	if (this.imageData == null)
	{
		this.createImageDataBuffer();
	}
	if (this.imageData == null)
	{
		return false;
	}
	else
	{
		if (pt.x < 0 || pt.y < 0 || pt.x >= this.image.width || pt.y >= this.image.height)
			return false;
		var index = (pt.y * this.image.width + pt.x) * 4 + 3;
		var alpha = this.imageData.data[index];
		return (alpha != 0);
	}
}

ImageWrapper.prototype.createImageDataBuffer = function()
{
	if (this.loaded && this.imgMgr)
	{
		var ctx = this.imgMgr.ctx;
		ctx.clearRect(0, 0, this.image.width, this.image.height);
		ctx.drawImage(this.image, 0, 0);
		try
		{
			try
			{
				this.imageData = ctx.getImageData(0, 0, this.image.width, this.image.height);
			}
			catch(e)
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
				this.imageData = ctx.getImageData(0, 0, this.image.width, this.image.height);
			}
		}
		catch(e)
		{
			throw new Error("unable to access image data: " + e);
		}
	}
}

ImageWrapper.prototype.isLoaded = function()
{
	return this.loaded;	
}

ImageWrapper.prototype.onload = function()
{
	this.loaded = true;
	this.width = this.image.width;
	this.height = this.image.height;
}

ImageWrapper.prototype.draw = function(ctx, x, y)
{
	if (this.loaded)
		ctx.drawImage(this.image, x, y);
}
// bitblt
ImageWrapper.prototype.drawEx = function(ctx, dstX, dstY, srcX, srcY, w, h)
{
	if (this.loaded)
		ctx.drawImage(this.image, srcX, srcY, w, h, dstX, dstY, w, h);
}

ImageWrapper.prototype.getImage = function()
{
    if (this.loaded)
        this.image;
    else
        return null;
}

function UrlImageManager()
{
    if (this instanceof UrlImageManager)
    {
        this.canvasBuffer = document.createElement("canvas");
        this.canvasBuffer.width = IMG_BUFFER_WIDTH;
        this.canvasBuffer.height = IMG_BUFFER_HEIGHT;
        this.ctx = this.canvasBuffer.getContext("2d");
    }
    else
    {
        return new UrlImageManager();
    }
}

UrlImageManager.prototype.imgs = {};

UrlImageManager.prototype.getImage = function(url)
{
    if (this.imgs[url] == null)
    {
        this.imgs[url] = new ImageWrapper(url, this);
    }
    return this.imgs[url];
}